package com.xyrl.project.manager.system.controller;

import com.xyrl.common.base.exception.XyrlException;
import com.xyrl.common.core.controller.BaseController;
import com.xyrl.project.common.annotation.Log;
import com.xyrl.project.common.system.service.DictionaryService;
import com.xyrl.project.common.system.vo.DictionaryVo;
import com.xyrl.project.manager.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("dict")
public class DictController extends BaseController {

    private String message;

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping
//    @RequiresPermissions("dict:view")
    public List<DictionaryVo> dictList(DictionaryVo vo) {
        System.out.println(ShiroUtil.loginAccount());
        return dictionaryService.findDictionaryTree(vo.getId());
    }

    @Log("新增字典")
    @PostMapping
    @RequiresPermissions("dict:add")
    public void addDict(@Valid DictionaryVo vo) throws XyrlException {
        try {
            vo.setUserId(ShiroUtil.loginUserId());
            dictionaryService.addDictionary(vo);
        } catch (XyrlException e) {
            log.error("添加数据字典异常:", e);
            throw e;
        } catch (Exception e) {
            message = "添加数据字典异常";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

    @Log("删除字典")
    @DeleteMapping("/{id}")
    @RequiresPermissions("dict:delete")
    public void delDict(@NotNull(message = "{required}") @PathVariable("id") Long id) throws XyrlException {
        try {
            dictionaryService.delDictionary(id, ShiroUtil.loginUserId());
        } catch (Exception e) {
            message = "删除字典失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

    @Log("修改字典")
    @PutMapping
    @RequiresPermissions("dict:update")
    public void updateDict(@Valid DictionaryVo vo) throws XyrlException {
        try {
            vo.setUserId(ShiroUtil.loginUserId());
            dictionaryService.modieyDictionary(vo);
        } catch (Exception e) {
            message = "修改字典失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

    @Log("修改字典")
    @PutMapping("enableOrUnEnable")
    @RequiresPermissions("dict:enableOrUnEnable")
    public void enableOrUnEnable(@NotNull(message = "{required}") Long id, @NotBlank(message = "{required}") String status) throws XyrlException {
        try {
            dictionaryService.editDictionaryStatus(id, status, ShiroUtil.loginUserId());
        } catch (Exception e) {
            message = "修改字典失败";
            log.error(message, e);
            throw new XyrlException(message);
        }
    }

}
